import { snippets } from "@/lib/generated/snippets";
import { Snippet } from "@/components/code";
import { Callout, Card, Cards, Steps, Tabs } from "nextra/components";
import UniversalTabs from "@/components/UniversalTabs";

# Running with Results

> This example assumes we have a [task](./your-first-task.mdx) registered on a running [worker](./workers.mdx).

One method for running a task in Hatchet is to run it and wait for its result. Some example use cases for this type of task trigger include:

1. Fanout patterns, where a parent fans out work to a number of children, and wants to receive the results of those child tasks and make some decision based on them. For example, if each child run fips a coin, and the parent wants to count up how many heads there were and do something with that information.
2. Waiting for long-running API calls to complete, such as if calling an LLM. For instance, if you had a part of your product that writes a poem for a user, your backend might run a `write_poem` task, which in turn calls an LLM, and then your backend would wait for that task to complete and return its result (the poem).

<UniversalTabs items={["Python", "Typescript", "Go"]}>
  <Tabs.Tab title="Python">

You can use your `Task` object to run a task and wait for it to complete by calling the `run` method. This method will block until the task completes and return the result.

    <Snippet src={snippets.python.child.trigger.running_a_task} />

You can also `await` the result of `aio_run`:

    <Snippet src={snippets.python.child.trigger.running_a_task_aio} />

Note that the type of `input` here is a Pydantic model that matches the input schema of your workflow.

  </Tabs.Tab>
  <Tabs.Tab title="Typescript">
  You can use your `Task` object to run a task and wait for it to complete by calling the `run` method. This method will return a promise that resolves when the task completes and returns the result.
    <Snippet src={snippets.typescript.simple.run.running_a_task} />
  </Tabs.Tab>
  <Tabs.Tab title="Go">
    You can use your `Task` object to run a task and wait for it to complete by calling the `Run` method. This method will block until the task completes and return the result.
    <Snippet src={snippets.go.simple.main.running_a_task} />
  </Tabs.Tab>
</UniversalTabs>

## Spawning Tasks from within a Task

You can also spawn tasks from within a task. This is useful for composing tasks together to create more complex workflows, fanning out batched tasks, or creating conditional workflows.

<UniversalTabs items={["Python", "Typescript", "Go"]}>
<Tabs.Tab title="Python">

You can run a task from within a task by calling the `aio_run` method on the task object from within a task function. This will associate the runs in the dashboard for easier debugging.

<Snippet
  src={snippets.python.child.simple_fanout.running_a_task_from_within_a_task}
/>

And that's it! The parent task will run and spawn the child task, and then will collect the results from its tasks.

</Tabs.Tab>
  <Tabs.Tab title="Typescript">

You can run a task from within a task by calling the `runChild` method on the `ctx` parameter of the task function. This will associate the runs in the dashboard for easier debugging.

    <Snippet src={snippets.typescript.simple.run.spawning_tasks_from_within_a_task} />

  </Tabs.Tab>
  <Tabs.Tab title="Go">

You can run a task from within a task by calling the `Run` method on the task object from within a task function. This will associate the runs in the dashboard for easier debugging.

    <Snippet src={snippets.go.simple.main.spawning_tasks_from_within_a_task} />

  </Tabs.Tab>
</UniversalTabs>

## Running Tasks in Parallel

Sometimes you may want to run multiple tasks concurrently. Here's how to do that in each language:

<UniversalTabs items={["Python", "Typescript", "Go"]}>
<Tabs.Tab title="Python">

Since the `aio_run` method returns a coroutine, you can spawn multiple tasks in parallel and await using `asyncio.gather`.

<Snippet src={snippets.python.child.trigger.running_multiple_tasks} />

</Tabs.Tab>
<Tabs.Tab title="Typescript">

Since the `run` method returns a promise, you can spawn multiple tasks in parallel and await using `Promise.all`.

<Snippet src={snippets.typescript.simple.run.running_multiple_tasks} />

</Tabs.Tab>
<Tabs.Tab title="Go">

You can run multiple tasks in parallel by calling `Run` multiple times in goroutines and using a `sync.WaitGroup` to wait for them to complete.

<Snippet src={snippets.go.simple.main.running_multiple_tasks} />

</Tabs.Tab>
</UniversalTabs>

<Callout type="info">
  While you can run multiple tasks in parallel using the `Run` method, this is
  not recommended for large numbers of tasks. Instead, we recommend using [bulk
  run methods](./bulk-run.mdx) for large parallel task execution.
</Callout>
